<template>
  <div class="admin-panel">
    <div class="admin-sidebar">
      <div class="sidebar-header">
        <h2>管理后台</h2>
      </div>
      <nav class="sidebar-nav">
        <router-link to="/admin" class="nav-item" exact-active-class="active">
          <el-icon><house /></el-icon>
          概览
        </router-link>
        <router-link to="/admin/users" class="nav-item" active-class="active">
          <el-icon><user /></el-icon>
          用户管理
        </router-link>
        <router-link to="/admin/games" class="nav-item" active-class="active">
          <el-icon><present /></el-icon>
          游戏管理
        </router-link>
        <router-link to="/admin/forum" class="nav-item" active-class="active">
          <el-icon><chat-dot-round /></el-icon>
          论坛管理
        </router-link>
        <router-link to="/admin/analytics" class="nav-item" active-class="active">
          <el-icon><data-analysis /></el-icon>
          数据分析
        </router-link>
        <router-link to="/" class="nav-item">
          <el-icon><back /></el-icon>
          返回首页
        </router-link>
      </nav>
    </div>
    
    <div class="admin-content">
      <div class="content-header">
        <h1>管理概览</h1>
        <div class="user-info">
          <span>管理员：{{ user.username }}</span>
          <button class="logout-btn" @click="logout">退出</button>
        </div>
      </div>
      
      <div class="stats-grid">
        <div class="stat-card">
          <div class="stat-icon users">
            <el-icon><user /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ stats.totalUsers }}</h3>
            <p>总用户数</p>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon games">
            <el-icon><present /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ stats.totalGames }}</h3>
            <p>总游戏数</p>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon comments">
            <el-icon><chat-dot-round /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ stats.totalComments }}</h3>
            <p>总评论数</p>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon ratings">
            <el-icon><star /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ stats.totalRatings }}</h3>
            <p>总评分数</p>
          </div>
        </div>
      </div>
      
      <div class="recent-activities">
        <h2>最近活动</h2>
        <div class="activities-list">
          <div class="activity-item">
            <div class="activity-icon">
              <el-icon><user /></el-icon>
            </div>
            <div class="activity-content">
              <p>新用户注册</p>
              <span class="activity-time">2 分钟前</span>
            </div>
          </div>
          
          <div class="activity-item">
            <div class="activity-icon">
              <el-icon><present /></el-icon>
            </div>
            <div class="activity-content">
              <p>新游戏发布</p>
              <span class="activity-time">5 分钟前</span>
            </div>
          </div>
          
          <div class="activity-item">
            <div class="activity-icon">
              <el-icon><chat-dot-round /></el-icon>
            </div>
            <div class="activity-content">
              <p>用户发表评论</p>
              <span class="activity-time">10 分钟前</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'AdminPanel',
  setup() {
    const router = useRouter()
    const user = ref(JSON.parse(localStorage.getItem('user')))
    const stats = ref({
      totalUsers: 0,
      totalGames: 0,
      totalComments: 0,
      totalRatings: 0
    })
    
    const loadStats = async () => {
      try {
        // 这里可以调用API获取统计数据
        // 暂时使用模拟数据
        stats.value = {
          totalUsers: 1250,
          totalGames: 342,
          totalComments: 5678,
          totalRatings: 3421
        }
      } catch (error) {
        console.error('加载统计数据失败:', error)
      }
    }
    
    const logout = () => {
      localStorage.removeItem('user')
      localStorage.removeItem('token')
      ElMessage.success('退出登录成功')
      router.push('/login')
    }
    
    onMounted(() => {
      loadStats()
    })
    
    return {
      user,
      stats,
      logout
    }
  }
}
</script>

<style scoped>
.admin-panel {
  display: flex;
  min-height: 100vh;
  background: #f5f5f5;
}

.admin-sidebar {
  width: 250px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  position: fixed;
  height: 100vh;
  left: 0;
  top: 0;
  overflow-y: auto;
}

.sidebar-header {
  padding: 2rem 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: bold;
}

.sidebar-nav {
  padding: 1rem 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  color: white;
  text-decoration: none;
  padding: 1rem 1.5rem;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  border-left-color: white;
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.2);
  border-left-color: white;
}

.admin-content {
  flex: 1;
  margin-left: 250px;
  padding: 2rem;
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.content-header h1 {
  color: #333;
  font-size: 2rem;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logout-btn {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(240, 147, 251, 0.4);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
  margin-bottom: 3rem;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 2rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  color: white;
}

.stat-icon.users {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.games {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.comments {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.ratings {
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

.stat-info h3 {
  font-size: 2rem;
  margin: 0;
  color: #333;
}

.stat-info p {
  margin: 0.5rem 0 0 0;
  color: #666;
  font-size: 0.9rem;
}

.recent-activities {
  background: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.recent-activities h2 {
  color: #333;
  margin-bottom: 1.5rem;
}

.activities-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.activity-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  border-left: 3px solid #667eea;
  background: #f8f9fa;
  border-radius: 8px;
}

.activity-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #667eea;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.activity-content p {
  margin: 0;
  color: #333;
  font-weight: 500;
}

.activity-time {
  color: #666;
  font-size: 0.8rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-sidebar {
    width: 200px;
  }
  
  .admin-content {
    margin-left: 200px;
    padding: 1rem;
  }
  
  .content-header {
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .admin-sidebar {
    width: 100%;
    position: relative;
    height: auto;
  }
  
  .admin-content {
    margin-left: 0;
  }
}
</style>